package com.czh.mybatisplus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //定义拦截器，添加顺序：多租户、动态表名 --> 分页、乐观锁 --> SQL 性能规范、防止全表更新与删除
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //动态表名(不建议使用，一改全局影响很大)
//        interceptor.addInnerInterceptor(new DynamicTableNameInnerInterceptor(new TableNameHandler() {
//            @Override
//            public String dynamicTableName(String sql, String tableName) {
//                //表名截取
//                tableName = "tb_"+tableName.substring(0,tableName.indexOf("_"));
//                //返回真正的表名
//                return tableName;
//            }
//        }));
        //开启乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //开启分页Page
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 数据变动记录插件，配置安全阈值，例如限制批量更新或插入的记录数不超过 x 条（少用）
        interceptor.addInnerInterceptor(new DataChangeRecorderInnerInterceptor().setBatchUpdateLimit(100));
        // 添加非法SQL拦截器（非法SQL拦截插件,不好用）
//        interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
        //防全表更新与删除插件
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

}
